package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Normalizer {
	static double maxValue,minValue;
	static int index,endIndex;

	private static String[] readFile(){
		try {

			File file = new File("F:/set.txt");
			FileReader fr=  new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			StringBuilder linha,dados;
			dados = new StringBuilder();
			String aux;
			while((aux = br.readLine())!=null){
				linha = new StringBuilder(aux);
				dados.append(linha); 
			}
			StringTokenizer st = new StringTokenizer(dados.toString(),"$$$$");
			ArrayList<String> lista = new ArrayList<String>();
			while(st.hasMoreElements()){
				lista.add(st.nextToken());
			}
			String[] result = new String[lista.size()]; 
			return lista.toArray(result);


		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;	
	} 

	public static void main(String[] args) {
		normalize("DSSTox_Generic_SID");
		System.out.println("maximo:"+maxValue+"\nminimo:"+minValue);
	}

	private static double getValue(String exemplo,String busca){
		busca = "<"+busca+">";
		int indiceIni = exemplo.indexOf(busca) + busca.length();
		String aux="";
		Character a=' ';
		int i= indiceIni;
		index = i;
		while((Character.isDigit(( a = exemplo.charAt(i))))){
			aux += a;
			i++;
		}
		endIndex = i;
		return Double.parseDouble(aux);
	}

	public static void normalize(String parameter){
		findValues(parameter);
		String busca = "<"+parameter+">";
		String exemplos[] = readFile();
		StringBuilder normalizado = new StringBuilder();
		for(String exemplo:exemplos){
			StringBuilder aux = new StringBuilder(exemplo);
			double value =  getValue(exemplo,busca);
			double normalized = (maxValue-value)/(maxValue-minValue);
			aux.replace(index, endIndex, normalized+"");
			normalizado.append(aux);
		}
		try{
			File saida = new File("F:/saida.txt");
			FileWriter fr = new FileWriter(saida);
			BufferedWriter bw = new BufferedWriter(fr);			
			bw.write(normalizado.toString());
			bw.close();
			
		}catch(IOException e){
			System.out.println("fudeu");
		}
	}

	public static void findValues(String name){
		maxValue = Double.MIN_VALUE;
		minValue = Double.MAX_VALUE;
		System.out.println("lendo dados");
		String[] exemplos = readFile();

		System.out.println("dados lidos");
		for(String exemplo :exemplos){
			System.out.println(exemplo);
			String busca = "<"+name+">";
			double value = getValue(exemplo, busca);
			if (value > maxValue){
				maxValue = value;
			}
			if (value < minValue){
				minValue = value; 
			}		
		}
	}

}